﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using System.Data.Entity;
using Abp.UI;
using Kendo.DynamicLinq;
using KPW.Operations.Project.Dto;

namespace KPW.Operations.Project
{
    
   // [AbpAuthorize(AppPermissions.Pages_Tenant_Test)]
    public class TestAppService : AbpZeroTemplateAppServiceBase, ITestAppService
    {
        private readonly IRepository<Test, long> _testRepository;
        public TestAppService(IRepository<Test,long> testRepository)
        {
            _testRepository = testRepository;
            
        }
        /// <summary>
        /// kendo获取列表方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public DataSourceResult KendoGetTests(DataSourceRequest input)
        {
        
           
           try
            {
                if (input.Filter != null)
                {
                    if (input.Filter.Filters.Count() <= 0)
                    {
                        input.Filter = null;
                    }
                }
                 var tests = _testRepository
            .GetAll()
            .OrderBy(p => p.Id)
            .AsQueryable()
            .ToDataSourceResult(input);
            var map =  tests.Data.MapTo<List<TestListDto>>();
            tests.Data = map;
           return tests;
            }
            catch (Exception ex)
            {

                throw new UserFriendlyException("传入格式错误");
            }
        }
        
        
        /// <summary>
        /// 无条件获取所有数据
        /// </summary>
        /// <returns></returns>
         public async Task<ListResultOutput<TestListDto>> GetTestsAsync()
        {
            try
            {
               var query = await _testRepository.GetAll().OrderBy(p => p.Id).ToListAsync();

               return new ListResultOutput<TestListDto>(query.MapTo<List<TestListDto>>());
            }
            catch (Exception ex)
            {

                throw new UserFriendlyException("展示数据错误" );
            }
        }
        
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Test_Delete)]
        /// <summary>
        /// 本身删除方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task DeleteTest(IdInput input)
        {
               try
            {
                  await _testRepository.DeleteAsync(input.Id);

            }
            catch (Exception ex)
            {

                throw new UserFriendlyException("传入格式类型错误");
            }
        }
        
        /// <summary>
        /// 插入与更新方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrUpdateTest(CreateOrUpdateTestInput input)
        {
            if (input.Test.ID.HasValue)
            {
                await UpdateTestAsync(input);
            }
            else
            {
                await CreateTestAsync(input);
            }
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Test_Create)]
      /// <summary>
      /// 插入一条本身对象数据
      /// </summary>
      /// <param name="input"></param>
      /// <returns></returns>
        public async Task CreateTestAsync(CreateOrUpdateTestInput input)
        {
          
            try
            {
                  var test = input.Test.MapTo<Test>();
                 await _testRepository.InsertAsync(test);
            }
            catch (Exception ex)
            {

                throw new UserFriendlyException("传入格式类型或关联外键错误");
            }
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Test_Update)]
      /// <summary>
      ///  更新一条本身对象数据
      /// </summary>
      /// <param name="input"></param>
      /// <returns></returns>
        public async Task UpdateTestAsync(CreateOrUpdateTestInput input)
        {
            var test = input.Test.MapTo<Test>();
            await _testRepository.UpdateAsync(test);
        }
        
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_Test_Update)]
      /// <summary>
      /// 更新加载前数据本身对象
      /// </summary>
      /// <param name="input"></param>
      /// <returns></returns>
        public async Task<GetTestForEditOutput> GetTestForEdit(NullableIdInput input)
        {
            try
            {
               Test test = null;
                if (input.Id.HasValue) //Editing existing role?
                {
                       test = await _testRepository.FirstOrDefaultAsync(input.Id.Value);
                }
                else
                {
                    throw new UserFriendlyException("无效ID");
                }
                return new GetTestForEditOutput
                {
                    Test = test.MapTo<TestEditDto>()
                };
            }
            catch (Exception ex)
            {

                throw new UserFriendlyException("展示数据错误" );
            }
        }
        /// <summary>
        ///        
        ///根据id查看单条记录(加载或者详情显示外键)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ListResultOutput<TestListDto>> GetTestsByID(IdInput input)
        {
            try
            {
                var query = await _testRepository.GetAll().Where(x => x.Id == input.Id).ToListAsync();

                return new ListResultOutput<TestListDto>(query.MapTo<List<TestListDto>>());
            }
            catch (Exception ex)
            {

                throw new UserFriendlyException("传入id无数值操作错误" );
            }
        }
    }
}

